package com.hitqz.robot.biz.flow.component.oilSample;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.enums.BrandEnum;
import com.hitqz.robot.api.business.enums.EquipmentEnum;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.dto.EquipmentParamsDto;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author xupkun
 * @date 2024/11/8
 */
@Slf4j
@LiteflowComponent(value = "WaitForResetStatusComponent", name = "[取油样机器人]等待复位状态完成")
@RequiredArgsConstructor
public class WaitForResetStatusComponent extends NodeComponent {

    private final RobotEquipmentService robotEquipmentService;

    private final EquipmentFactory equipmentFactory;


    @Override
    public void process() throws Exception {
        FlowContextDto dto = this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        log.info("任务数据 {}", task.getParseOrderParams());
        List<RobotEquipment> oils = robotEquipmentService.list(Wrappers.<RobotEquipment>lambdaQuery()
                //.eq(RobotEquipment::getType, EquipmentEnum.PLC.getType())
                .eq(RobotEquipment::getType, EquipmentEnum.CAR_PLC.getType())
                .eq(RobotEquipment::getBrand, BrandEnum.CAR_PLC.getEnStr())
                .eq(RobotEquipment::getIsRecordParam, 1));
        for (RobotEquipment oil : CollUtil.emptyIfNull(oils)) {
            // 车上装开始对接
            IEquipmentService service = equipmentFactory.getServiceByBrand(oil.getBrand());
            QueryParamsDto queryParamsDto = new QueryParamsDto();
            queryParamsDto.setPassword(oil.getPassword());
            queryParamsDto.setUsername(oil.getUsername());
            queryParamsDto.setPort(oil.getPort());
            queryParamsDto.setChannel(oil.getChannel());
            queryParamsDto.setIp(oil.getIp());
            //service.executeCmd(queryParamsDto , false , EquipmentCmdEnum.FET_OIL, fetchOilDto);
            List<EquipmentParamsDto> paramsDtos = service.getEquipmentParams(queryParamsDto);
            //while (true) {
            //    Optional<EquipmentParamsDto> resetStatus = paramsDtos.stream().filter(t-> Objects.equals(t.getKey(), "reset")).findFirst();
            //    if (!resetStatus.isPresent()) {
            //        log.info("没有复位状态");
            //        break;
            //    }
            //    if (Objects.equals(resetStatus.get().getValue(), "1")) {
            //        log.info("车上PLC复位完成");
            //        break;
            //    }
            //    paramsDtos = service.getEquipmentParams(queryParamsDto);
            //}
        }
        List<RobotEquipment> plcOils = robotEquipmentService.list(Wrappers.<RobotEquipment>lambdaQuery()
                //.eq(RobotEquipment::getType, EquipmentEnum.PLC.getType())
                .eq(RobotEquipment::getType, EquipmentEnum.OIL_PLC.getType())
                .eq(RobotEquipment::getBrand, BrandEnum.OIL_PLC.getEnStr())
                .eq(RobotEquipment::getIsRecordParam, 1));
        for (RobotEquipment plcOil : CollUtil.emptyIfNull(plcOils)) {
            // 车上装开始对接
            IEquipmentService service = equipmentFactory.getServiceByBrand(plcOil.getBrand());
            QueryParamsDto queryParamsDto = new QueryParamsDto();
            queryParamsDto.setPassword(plcOil.getPassword());
            queryParamsDto.setUsername(plcOil.getUsername());
            queryParamsDto.setPort(plcOil.getPort());
            queryParamsDto.setChannel(plcOil.getChannel());
            queryParamsDto.setIp(plcOil.getIp());
            List<EquipmentParamsDto> paramsDtos = service.getEquipmentParams(queryParamsDto);
            while (true) {
                Optional<EquipmentParamsDto> resetStatus = paramsDtos.stream().filter(t-> Objects.equals(t.getKey(), "reset")).findFirst();
                if (!resetStatus.isPresent()) {
                    log.info("没有复位状态");
                    break;
                }
                if (Objects.equals(resetStatus.get().getValue(), "1")) {
                    log.info("取油柜子PLC复位完成");
                    break;
                }
                paramsDtos = service.getEquipmentParams(queryParamsDto);
            }
        }
    }
}
